home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / libdemo / timer.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  241 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "timer.h"
  18. #include "stdio.h"
  19. #include <gl.h>
  20. #include <gl/get.h>
  21.  
  22.  
  23. timer::timer()
  24. {
  25.     start();
  26. }
  27.  
  28. void
  29. timer::start()
  30. {
  31.     running = 1;
  32.     gettimeofday(&beginTime, 0);
  33. }
  34.  
  35. void
  36. timer::stop()
  37. {
  38.     running = 0;
  39.     gettimeofday(&endTime, 0);
  40. }
  41.  
  42. float
  43. timer::elapse()
  44. {
  45.     if (running)
  46.     gettimeofday(&endTime, 0);
  47.  
  48.     float e = endTime.tv_sec - beginTime.tv_sec +
  49.           (endTime.tv_usec - beginTime.tv_usec) / 1000000.0;
  50.  
  51. //    if (e <= 0.0)
  52. //    return 0.000001;
  53. //    else
  54.     return e;
  55. }
  56.  
  57. float
  58. timer::elapseStart()
  59. {
  60.     float e = elapse();
  61.     beginTime.tv_sec = endTime.tv_sec;
  62.     beginTime.tv_usec = endTime.tv_usec;
  63.  
  64.     return e;
  65. }
  66.  
  67.  
  68. totalTimer::totalTimer(char * d)
  69. {
  70.     description = d;
  71.     totalTime = 0;
  72.     count = 0;
  73. }
  74.  
  75. void
  76. totalTimer::stop()
  77. {
  78.     timer::stop();
  79.     totalTime += elapse();
  80.     count++;
  81. }
  82.  
  83. float
  84. totalTimer::getTotal()
  85. {
  86.     return totalTime;
  87. }
  88.  
  89. int
  90. totalTimer::getCount()
  91. {
  92.     return count;
  93. }
  94.  
  95. void
  96. perfTimer::print()
  97. {
  98.     fprintf(stderr, "%30s: total %6.2f in %0d samples.\n",
  99.         description, getTotal(), getCount());
  100. }
  101.  
  102. perfTimer::~perfTimer()
  103. {
  104.     print();
  105. }
  106.  
  107. /*---------------------------------------------------------------------------
  108.     Frame Rate Stuff
  109.   ---------------------------------------------------------------------------*/
  110.  
  111. char * defaultRateDescription = "%2.0f Frames/Second";
  112. char * rateDescription;
  113. float rateMultiplier = 1;
  114. int rateActive = 1;
  115. float rateConvergence = 1.0;
  116. int useCmapMode = 0;
  117.  
  118. float theFrameRate;
  119.  
  120. timer * frameRateTimer = 0;
  121.  
  122. extern "C" void
  123. showFrameRate()
  124. {
  125.     if (frameRateTimer == 0) {
  126.     frameRateTimer = new timer;
  127.     rateDescription = defaultRateDescription;
  128.  
  129.     long mode = getdisplaymode();
  130.     if ((mode == DMSINGLE) || (mode == DMDOUBLE))
  131.         useCmapMode = TRUE;
  132.     }
  133.     else
  134.     if (rateActive) {
  135.  
  136.     Boolean oldZbuf = getzbuffer();
  137.     zbuffer(FALSE);
  138.  
  139.     long oldMode = getmmode();
  140.     Matrix oldProjection;
  141.     if (oldMode == MSINGLE)
  142.         pushmatrix();
  143.     else
  144.     {
  145.         static Matrix Identity = {
  146.         { 1, 0, 0, 0 },
  147.         { 0, 1, 0, 0 },
  148.         { 0, 0, 1, 0 },
  149.         { 0, 0, 0, 1 }
  150.         };
  151.  
  152.         mmode(MVIEWING);
  153.         pushmatrix();
  154.         loadmatrix(Identity);
  155.  
  156.         mmode(MPROJECTION);
  157.         getmatrix(oldProjection);
  158.     }
  159.  
  160.     long winX, winY;
  161.     getsize(&winX, &winY);
  162.     ortho2(0, (int)winX -1, 0, (int)winY -1);
  163.  
  164. //    pushviewport();
  165. //    viewport(0, (short)winX -1, 0, (short)winY -1);
  166.  
  167.     float t = frameRateTimer->elapseStart();
  168.     float rate = 1 / t;
  169.     float newPortion = t / rateConvergence;
  170.     if (newPortion > 1)
  171.         newPortion = 1;
  172.     theFrameRate = rate * newPortion + theFrameRate * (1 - newPortion);
  173.  
  174.     char buffer[256];
  175.     sprintf(buffer, rateDescription, theFrameRate * rateMultiplier);
  176.  
  177.     cmov(10, 10, 0);
  178.     if (useCmapMode)
  179.         color(4);
  180.     else
  181.         cpack(0x00ff0000);
  182.     charstr(buffer);
  183.     cmov(12, 12, 0);
  184.     if (useCmapMode)
  185.         color(7);
  186.     else
  187.         cpack(0x00ffffff);
  188.     charstr(buffer);
  189.  
  190.     if (oldMode == MSINGLE)
  191.         popmatrix();
  192.     else
  193.     {
  194.         loadmatrix(oldProjection);
  195.  
  196.         mmode(MVIEWING);
  197.         popmatrix();
  198.  
  199.         mmode((short)oldMode);
  200.     }
  201.  
  202.     zbuffer(oldZbuf);
  203. //    popviewport();
  204.     }
  205. }
  206.  
  207.  
  208. extern "C" void
  209. setFrameRateMultiplier(float m)
  210. {
  211.     rateMultiplier = m;
  212. }
  213.  
  214.  
  215. extern "C" void
  216. setFrameRateDescription(char * d)
  217. {
  218.     rateDescription = d;
  219. }
  220.  
  221.  
  222. extern "C" void
  223. setFrameRateActive(int a)
  224. {
  225.     rateActive = a;
  226. }
  227.  
  228.  
  229. extern "C" void
  230. setFrameRateConvergence(float c)
  231. {
  232.     rateConvergence = c;
  233. }
  234.  
  235.  
  236. extern "C" void
  237. setFrameRateCmapMode(int t)
  238. {
  239.     useCmapMode = t;
  240. }
  241.